﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sim
{
    public partial class Increased : Form
    {
        public Increased()
        {
            InitializeComponent();
        }

        //退出按钮
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //增加按钮
        private void button1_Click(object sender, EventArgs e)
        {

            //判断书名是否重复
            string checkTitleSql = "select * from Articles where Title='{0}'";

            checkTitleSql = string.Format(checkTitleSql, textBoxTitle.Text);

            var dtCheckTitle = DbHelper.GetDbHelper(checkTitleSql);

            if (dtCheckTitle.Rows.Count != 0)
            {
                //书名重复，则不执行注册操作
                MessageBox.Show("书名已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            else
            {
                var TTitle = string.IsNullOrEmpty(textBoxTitle.Text);//判断书名是否为空

                var TAuthor = string.IsNullOrEmpty(textBoxAuthor.Text);//判断作者是否为空

                if (TTitle)
                {
                    MessageBox.Show("书名不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                else if (TAuthor)
                {
                    MessageBox.Show("作者不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                if (!TTitle && !TAuthor)
                {
                    var res = MessageBox.Show("是否添加？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (res.Equals(DialogResult.Yes))
                    {
                        string sql = "insert into Articles(Title,Author) values('{0}','{1}')";

                        sql = string.Format(sql, textBoxTitle.Text, textBoxAuthor.Text);

                        var dtInsert = DbHelper.AddOrInsertOrUpdate(sql);

                        if (dtInsert)
                        {
                            MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("添加失败！自己找原因！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        MessageBox.Show("取消成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                }
            }
        }
    }
}
